/* 
 * File:   estat_main.c
 * Author: oddi
 *
 * Created on January 20, 2010, 11:38 AM
 */

#include <stdio.h>

#include "statistics.h"
#include <stdlib.h>
#include "initialize_config.h"
#include "global_definitions.h"
#include "math.h"
#include "solver.h"

/*
 * 
 */

double pulse_filling(fsp_t* fsp, double time)
{
    return sin((fsp->csimp->w_ce)*time);
}

double pulse_time_envelope(fsp_t* fsp, double time)
{
    simp_t* simp = fsp->simp;
    if ((time < simp->PulseDelay) || (time > simp->PulseDelay + simp->PulseDuration))
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

double pulse_z_envelope(fsp_t* fsp, double z)
{
    simp_t* simp = fsp->simp;
    if ((z < simp->PulseZShift) || (z > simp->PulseZShift + simp->PulseLength))
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int main(int argc, char** argv) {
    /*read cfg file*/
    char* cfg_file = NULL;
    if (argc > 1)
    {
        simp_t sim_param;
        csimp_t csim_param;
        fsp_t fsp;
        system_paramaters_t ode_system_parameters;
        int i = 0;
        fsp.simp = &sim_param;
        fsp.csimp = &csim_param;
        int result;
        cfg_file = argv[1];
        result = conf_read_simulation_config(cfg_file, &fsp);
        if (result == CONFIG_FALSE)
        {
            printf ("Simulation parameters initialization is failed\n");
        }
        else
        {
            printf ("Simulation parameters initialization is successful\n");
            conf_generate_calculated_parameters(&fsp, &pulse_time_envelope, &pulse_filling, &pulse_z_envelope);
            solver_initialize_ode_system(&ode_system_parameters, &fsp );
            solver_get_full_solution(&ode_system_parameters);
            
            
            statistics_cat_files(fsp.simp->savedir, fsp.simp->tempdir, fsp.simp->name_template);
            statistics_get_v_tr_speed_distribution(fsp.simp->tempdir, fsp.simp->name_template, 70);
            solver_free_resources_for_ode_system(&ode_system_parameters);
            conf_free_configuration(&fsp);
            return EXIT_SUCCESS;
        }
        
        return result;
    }
    else
    {
        printf("Configuration file is not defined!\n");
        return (EXIT_FAILURE);
    }
}

